package com.honglang.module.article.action;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;

import com.google.gson.Gson;
import com.honglang.common.tools.Constants;
import com.honglang.common.tools.HtmlCodeProcess;
import com.honglang.module.article.entity.BlogArticle;
import com.honglang.module.article.service.ArticleCategoriesService;
import com.honglang.module.article.service.BlogArticleManager;
import com.honglang.module.log.service.LoginLogService;
import com.honglang.module.user.entity.User;
import com.honglang.module.user.service.UserService;
import com.honglang.search.lucene.SearchEngineer;
import com.honglang.struts2.action.BaseAction;
import com.opensymphony.xwork2.Action;


public class BlogArticleAction extends BaseAction{

	private ArticleCategoriesService articleCategoriesService;
	
	private BlogArticleManager blogArticleManager;
	
	private UserService userService;
	
	private LoginLogService loginLogService;
	
	private SearchEngineer searchEngineer;
	
	private Gson gson;
	
	/**
	 * preView the artcle 
	 * @return
	 */
	public String articlePreView(){
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int pageNo = Constants.DEFAULT_PAGE_NO;
		if(null != pageNoStr){
			pageNo = Integer.valueOf(this.getRequest().getParameter("pageNo"));
		}
		this.getRequest().setAttribute("pager", this.blogArticleManager.getBlogArticleInPager(Constants.pageSize, pageNo));
		return Action.SUCCESS;
	}
	
	
	
	
	
	public String articleDetail(){
		String articleId = this.getRequest().getParameter("bid");
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int commentPageNo = Constants.DEFAULT_PAGE_NO;
		if(null != pageNoStr){
			commentPageNo = Integer.valueOf(pageNoStr);
		}
		Map<String,Object> map = this.getBlogArticleManager().getBlogArticleCommentPerPage(Constants.pageCommentSize,commentPageNo , Integer.valueOf(articleId));
		BlogArticle blogArticle =  this.blogArticleManager.getBlogAritcleById(Integer.valueOf(articleId));
		this.getRequest().setAttribute("blogArticle", blogArticle);
		this.getRequest().setAttribute("preArticle", this.getBlogArticleManager().getPreBlogArticle(blogArticle));
		this.getRequest().setAttribute("nextArticle", this.getBlogArticleManager().getNextBlogArticle(blogArticle));
		this.getRequest().setAttribute("pager", map.get("pager"));
		this.getRequest().setAttribute("commentList", map.get("commentList"));
		return Action.SUCCESS;
	}
	
	
	
	
	
	public String postComment() throws IOException{
		Map<String,String> map = new HashMap<String,String>();
		String quickLogin = this.getRequest().getParameter("quickLogin");
		map.put(Constants.Comment_Content, ((HtmlCodeProcess)this.getWebApplicationContext().getBean("htmlCodeProcess")).saveCodeFragment(this.getRequest().getParameter("commentContent")));
		map.put(Constants.BLOG_ARTICLE_ID,this.getRequest().getParameter("comment_post_ID"));
		map.put(Constants.BE_COMMENTED_ID, this.getRequest().getParameter("comment_parent"));
		if(null != quickLogin && "true".equals(quickLogin)){
			User user = new User();
			user.setUserName(this.getRequest().getParameter(Constants.USERNAME_KEY));
			user.setPassword(this.getRequest().getParameter("password"));
			if(this.getUserService().validateUser(user)){
				map.put(Constants.USERNAME_KEY,user.getUserName());
				user = this.getUserService().getUserByUserName(user.getUserName());
				this.getSession().setAttribute(Constants.USERNAME_KEY, user.getUserName());
				this.getSession().setAttribute(Constants.USER_KEY, user);
				this.getSession().setAttribute(Constants.Role, user.getRole());
				this.getLoginLogService().saveLoginLog(user);
				this.getBlogArticleManager().saveBlogArticleComment(this.getBlogArticleManager().createComment(map));
				this.getResponse().getWriter().write("true");
				return Action.NONE;
			}else{
				this.getResponse().getWriter().write("false");
				return Action.NONE;
			}
		}else{
			map.put(Constants.USERNAME_KEY,(String)this.getSession().getAttribute(Constants.USERNAME_KEY));
			String editCommentId = this.getRequest().getParameter("editCommentId");
			if(editCommentId != null && !editCommentId.trim().isEmpty()){
				this.getBlogArticleManager().updateCommentContent(map, Integer.valueOf(editCommentId));
			}else{
				this.getBlogArticleManager().saveBlogArticleComment(this.getBlogArticleManager().createComment(map));
			}
				this.getResponse().getWriter().write("true");
			return Action.NONE;
		}
	}
	
	
	public String ajaxGetComment() throws IOException{
		String commentId = this.getRequest().getParameter("commentId");
		this.getResponse().setHeader("Cache-Control", "no-cache");
		this.getResponse().setContentType("text/json;charset=utf-8");
		this.getResponse().getWriter().write(gson.toJson(this.getBlogArticleManager().getCommentById(commentId)));
		return Action.NONE;
	}
	
	
	
	/**
	 * search blogArticle by content and title;
	 * @return
	 */
	public String search(){
		String searchStr = this.getRequest().getParameter("searchStr");
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int pageNo = Constants.DEFAULT_PAGE_NO;
		if(null != pageNoStr){
			pageNo = Integer.valueOf(this.getRequest().getParameter("pageNo"));
		}
		//Query query  = this.getSearchEngineer().buildQuery(searchStr, "content");
		Query query  = this.getSearchEngineer().bulidQuery(searchStr, new String[]{"content","title"});
		Sort sort =this.getSearchEngineer().buildSort("date",SortField.LONG ,true);
		Map<String,Object> map = this.getSearchEngineer().search(query, sort,"content", this.getServletContext().getRealPath("/")+"search//index", BlogArticle.class, pageNo, Constants.SEARCH_PAGE_SIZE);
		this.getRequest().setAttribute("highlightList",map.get("highlightList"));
		this.getRequest().setAttribute("pager", map.get("pager"));
		this.getRequest().setAttribute("searchStr", searchStr);
		return Action.SUCCESS;
	}
	
	
	/**
	 * search BlogArticle by category name;
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public String getBlogArticlePerCategory() throws UnsupportedEncodingException{
		String categoryName = this.getRequest().getParameter("category");
		categoryName = new String(categoryName.getBytes("ISO-8859-1"), "UTF-8");
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int pageNo = Constants.DEFAULT_PAGE_NO;
		if(null != pageNoStr){
			pageNo = Integer.valueOf(this.getRequest().getParameter("pageNo"));
		}
		Query query  = this.getSearchEngineer().buildQuery(categoryName, "category");
		Sort sort =this.getSearchEngineer().buildSort("date",SortField.LONG ,true);
		this.getRequest().setAttribute("pager", this.getSearchEngineer().search(query, sort, this.getServletContext().getRealPath("/")+"search//index", BlogArticle.class, pageNo, Constants.SEARCH_PAGE_SIZE));
		this.getRequest().setAttribute("searchStr", categoryName);
		return Action.SUCCESS;
	}
	
	
	/**
	 * search BlogArticle by Tag;
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public String getBlogArticlePerTag() throws UnsupportedEncodingException{
		String tagName = this.getRequest().getParameter("tag");
		tagName = new String(tagName.getBytes("ISO-8859-1"), "UTF-8");
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int pageNo = Constants.DEFAULT_PAGE_NO;
		if(null != pageNoStr){
			pageNo = Integer.valueOf(this.getRequest().getParameter("pageNo"));
		}
		Query query  = this.getSearchEngineer().bulidQuery(tagName, new String[]{"tags"});
		Sort sort =this.getSearchEngineer().buildSort("date",SortField.LONG ,true);
		this.getRequest().setAttribute("pager", this.getSearchEngineer().search(query, sort, this.getServletContext().getRealPath("/")+"search//index", BlogArticle.class, pageNo, Constants.SEARCH_PAGE_SIZE));
		this.getRequest().setAttribute("searchStr", tagName);
		return Action.SUCCESS;
	}
	
	
	
	
	

	public SearchEngineer getSearchEngineer() {
		return searchEngineer;
	}

	public void setSearchEngineer(SearchEngineer searchEngineer) {
		this.searchEngineer = searchEngineer;
	}


	public ArticleCategoriesService getArticleCategoriesService() {
		return articleCategoriesService;
	}

	public void setArticleCategoriesService(
			ArticleCategoriesService articleCategoriesService) {
		this.articleCategoriesService = articleCategoriesService;
	}

	public BlogArticleManager getBlogArticleManager() {
		return blogArticleManager;
	}

	public void setBlogArticleManager(BlogArticleManager blogArticleManager) {
		this.blogArticleManager = blogArticleManager;
	}


	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}



	public LoginLogService getLoginLogService() {
		return loginLogService;
	}

	public void setLoginLogService(LoginLogService loginLogService) {
		this.loginLogService = loginLogService;
	}

	protected Gson getGson() {
		return gson;
	}

	public void setGson(Gson gson) {
		this.gson = gson;
	}

 }
